package com.fyzk.activity.lottery.jc.football.adapter;

import com.fyzk.activity.lottery.jc.football.JCInfo;
import com.fyzk.activity.lottery.jc.football.guoguan.JC_GuoGuanDialog;
import com.fyzk.adapter.JCBaseAdapter;
import com.fyzk.utils.LogUtil;

import java.util.Arrays;
import java.util.List;

public abstract class JCZQZhiFuBaseAdapter extends JCBaseAdapter {

    public static final String RESULT_MOREN = "";
    public static final String RESULT_SPLIT = ",";

    protected List<JCInfo> infors;

    /**
     * 获取比赛选择的比赛场数
     * @param infoData
     * @return
     */
    public int getSelCount(List<JCInfo> infoData){
        int selCount = 0;
        for (JCInfo info : infoData) {
            if (info.getOnclikNum() > 0) {
                selCount++;
            }
        }
        return selCount;
    }

    public void refreshDan() {
        int[] minmaxChuanGuans = getMinChuanGuan();
        int minChuanGuan = minmaxChuanGuans[0];
        int maxChuanGuan = minmaxChuanGuans[1];
        int danMax = minChuanGuan - 1;

        int selCount = getSelCount(infors);

        if (maxChuanGuan == selCount) {
            LogUtil.e("--refreshDan--最大串关数=场次数-不可设胆--");
            for (JCInfo infor : infors) {
                infor.setDan(false);
                infor.setDaneble(false);
            }
        } else {
            int danAmount = 0;
            for (JCInfo infor : infors) {
                if (infor.isDan()) {
                    danMax--;
                    LogUtil.e("infor.isDan()>" + infor.getHomeTeam());
                    danAmount++;
                }
            }
            if (danAmount >= minChuanGuan) {
                for (JCInfo infor : infors) {
                    LogUtil.e("--refreshDan--胆的个数超过选择的串关个数，胆重置，胆设置为有效--");
                    infor.setDan(false);
                    if(minChuanGuan!=0&&infor.getOnclikNum()>0){
                        infor.setDaneble(true);
                    }else{
                        infor.setDaneble(false);
                    }
                }
            } else {
                for (JCInfo infor : infors) {
                    if (danAmount > danMax) {
                        LogUtil.e("danAmount>danMax|" + danAmount + "|" + danMax);
                        if (danMax == 0) {
                            if (infor.isDan()) {
                                LogUtil.e("--refreshDan-----可以选择胆=当前已选胆数--切本赛事胆已选择--" + infor.getHomeTeam());
                                infor.setDaneble(true);
                            } else {
                                LogUtil.e("--refreshDan-----可以选择胆=当前已选胆数--赛事未选择--" + infor.getHomeTeam());
                                infor.setDaneble(false);
                            }
                        } else {
                            if(infor.getOnclikNum()>0){
                                infor.setDaneble(true);
                            }else{
                                infor.setDaneble(false);
                            }
                        }
                    } else {
                        LogUtil.e("--refreshDan-----可以选择胆>当前已选胆数---");
                        if(infor.getOnclikNum()>0){
                            infor.setDaneble(true);
                        }else{
                            infor.setDaneble(false);
                        }
                    }
                }
            }

        }
        notifyDataSetChanged();
    }

    private int[] getMinChuanGuan() {
        String chuan = JC_GuoGuanDialog.result;
        LogUtil.e(JC_GuoGuanDialog.result);
        int[] minmax = new int[2];
        if (chuan.contains("串")) {
            // 判断是否是多串M串N过关
            if(JC_GuoGuanDialog.isDuoChuan){
                String[] split = chuan.split("串");
                int teamNum = Integer.valueOf(split[0]);
                int selCount = getSelCount(infors);

                // 如果M != 选择的比赛场数
                if (teamNum != selCount) {
                    String duoChuan = JC_GuoGuanDialog.duo_result;
                    String[] duo_chuan_array = duoChuan.split(",");
                    if (duo_chuan_array.length > 0) {
                        int[] duo_chuans = new int[duo_chuan_array.length];
                        for (int i = 0; i < duo_chuan_array.length; i++) {
                            String duo_chuan_i = duo_chuan_array[i];
                            duo_chuans[i] = Integer.parseInt(duo_chuan_i.charAt(0) + "");
                        }
                        Arrays.sort(duo_chuans);
                        minmax[0] = duo_chuans[0];
                        minmax[1] = duo_chuans[duo_chuans.length - 1];
                        return minmax;
                    }else{
                        int min = Integer.parseInt(chuan.charAt(0) + "");
                        minmax[0] = min;
                        minmax[1] = min;
                        return minmax;
                    }
                }else{
                    int min = Integer.parseInt(chuan.charAt(0) + "");
                    minmax[0] = min;
                    minmax[1] = min;
                    return minmax;
                }
            }else{
                String[] chuan_array = chuan.split(",");
                if (chuan_array.length > 0) {
                    int[] chuans = new int[chuan_array.length];
                    for (int i = 0; i < chuan_array.length; i++) {
                        String chuan_i = chuan_array[i];
                        chuans[i] = Integer.parseInt(chuan_i.charAt(0) + "");
                    }
                    Arrays.sort(chuans);
                    minmax[0] = chuans[0];
                    minmax[1] = chuans[chuans.length - 1];
                    return minmax;
                } else {
                    int min = Integer.parseInt(chuan.charAt(0) + "");
                    minmax[0] = min;
                    minmax[1] = min;
                    return minmax;
                }
            }
        } else {
            return minmax;
        }

    }

}
